package com.elric.mall.common.utils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.json.JSONUtil;
import com.elric.mall.common.domain.ExcelStyle;
import com.elric.mall.common.domain.ImportBase;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 实现功能：cell样式处理工具类
 *
 * @author 作者[Yuxuan.he]
 * @version 1.0.00  2021-11-10 15:29
 */
public class ExcelStyleHandler {

    private Map<String, XSSFCellStyle> styleMap;

    private XSSFWorkbook workbook;

    public ExcelStyleHandler(XSSFWorkbook workbook) {
        this.workbook = workbook;
        this.styleMap = new HashMap<>();
    }

    /**
     * 根据excel样式对象生成唯一key
     * @param obj
     * @return
     */
    private String getKey(ExcelStyle obj) {
        if (!Objects.isNull(obj)) {
            return SecureUtil.sha1(JSONUtil.parse(obj).toString());
        }
        return null;
    }

    /**
     * 创建xss样式对象
     * @param obj
     * @return
     */
    private XSSFCellStyle create(ExcelStyle obj) {
        XSSFCellStyle hssfCellStyle = this.workbook.createCellStyle();
        hssfCellStyle.setAlignment(HorizontalAlignment.LEFT);
        hssfCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        //设置字体
        XSSFFont font = this.workbook.createFont();

        if(!Objects.isNull(obj.getFillForegroundColor())){
            hssfCellStyle.setFillForegroundColor(obj.getFillForegroundColor());
        }
        if (!Objects.isNull(obj.getFillPattern())){
            hssfCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        }
        if (obj.getFontSize() > 0){
            font.setFontHeightInPoints(obj.getFontSize());
        } else {
            font.setFontHeightInPoints((short) 10);
        }
        if(!Objects.isNull(obj.getAlignment())){
            hssfCellStyle.setAlignment(obj.getAlignment());
        }
        if (obj.isBorder()) {
            hssfCellStyle.setBorderBottom(BorderStyle.THIN);
            hssfCellStyle.setBorderLeft(BorderStyle.THIN);
            hssfCellStyle.setBorderRight(BorderStyle.THIN);
            hssfCellStyle.setBorderTop(BorderStyle.THIN);
        }
        if (StrUtil.isNotBlank(obj.getFontName())){
            font.setFontName(obj.getFontName());
        }
        hssfCellStyle.setWrapText(true);
        hssfCellStyle.setFont(font);
        return hssfCellStyle;
    }

    /**
     * 获取默认的表头样式
     * @return
     */
    public ExcelStyle getDefaultHeaderStyle(){
        ExcelStyle style = new ExcelStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setFontName("黑体");
        style.setFontSize((short) 11);
        style.setBorder(true);
        return style;
    }

    public XSSFCellStyle getDefaultStyle(){
        ExcelStyle obj = getDefaultHeaderStyle();
        String key = getKey(obj);

        // 如果不存在于map中，则手动创建新的cellsyle对象
        if (!this.styleMap.containsKey(key)){
            XSSFCellStyle xssfCellStyle = create(obj);
            this.styleMap.put(key, xssfCellStyle);
            return xssfCellStyle;
        }
        return this.styleMap.get(key);
    }

    /**
     * 获取XSSFCellStyle
     * @param obj
     * @param <T>
     * @return
     */
    public <T extends ImportBase> XSSFCellStyle getStyle(T obj){
        ExcelStyle style = new ExcelStyle();
        BeanUtil.copyProperties(obj, style);
        String key = getKey(style);

        // 如果不存在于map中，则手动创建新的cellsyle对象
        if (!this.styleMap.containsKey(key)){
            XSSFCellStyle xssfCellStyle = create(style);
            this.styleMap.put(key, xssfCellStyle);
            return xssfCellStyle;
        }
        return this.styleMap.get(key);
    }
}
